package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FileMapper;
import com.itheima.dao.PictureMapper;

import com.itheima.entity.PageResult;
import com.itheima.pojo.File;
import com.itheima.pojo.Picture;
import com.itheima.service.FileService;
import com.itheima.service.PictureService;

import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import oshi.jna.platform.mac.SystemB;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;


    @Autowired
    private IdWorker idWorker;

    @Autowired
    private FileMapper fileMapper;

    @Override
    @Transactional
    public void add(List<Map> pictureVOList) {
        //将map转换为实体类
        List<Picture> pictureList = JSON.parseArray(JSON.toJSONString(pictureVOList), Picture.class);
        System.out.println(pictureList);
        for (Picture picture : pictureList) {
            picture.setUid(idWorker.nextId() + "");
            picture.setCreateTime(new Date());
            picture.setUpdateTime(new Date());
            pictureMapper.insertSelective(picture);
        }
    }

    @Override
    @Transactional
    public void edit(Map pictureVO) {
        //将map转换成实体类
        Picture picture = JSON.parseObject(JSON.toJSONString(pictureVO), Picture.class);
        picture.setUpdateTime(new Date());
        pictureMapper.updateByPrimaryKeySelective(picture);
    }

    @Override
    @Transactional
    public void delete(Map pictureVO) {
        //将map转换成实体类
        Picture picture = JSON.parseObject(JSON.toJSONString(pictureVO), Picture.class);
        pictureMapper.delete(picture);
    }

    @Override
    public PageResult<Picture> getList(Map searchMap) {
        //获取当前页
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (null == currentPage || currentPage < 0) {
            currentPage = 0;
        }
        //设置默认每页显示条数
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (null == pageSize || pageSize <= 0) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);
        Example example = this.createExample(searchMap);
        Page<Picture> pageList = (Page<Picture>) pictureMapper.selectByExample(example);
        List<Picture> pictureList = pageList.getResult();
        if (pictureList == null || pictureList.size() <= 0) {
            throw new RuntimeException("未查询到符合条件的图片!");
        }
//        todo
        for (Picture picture : pageList) {
            String fileUid = picture.getFileUid();
            File file = fileMapper.selectByPrimaryKey(fileUid);
            if (file != null) {
                picture.setPictureUrl(file.getQiNiuUrl());
            }
        }
        PageResult<Picture> pageResult = new PageResult<>();
        pageResult.setRecords(pictureList);
        pageResult.setCurrent((long) pageList.getPageNum() + 1);
        pageResult.setOptimizeCountSql(true);
        pageResult.setSize((long) pageList.getPageSize());
        pageResult.setTotal(pageList.getTotal());
        pageResult.setSearchCount(true);
        return pageResult;
    }

    @Override
    @Transactional
    public void setCover(Map pictureVO) {
        //将map转换成实体类
        Picture picture = JSON.parseObject(JSON.toJSONString(pictureVO), Picture.class);
        picture = pictureMapper.selectOne(picture);
        if (picture == null) {
            throw new RuntimeException("该图片不存在!");
        }
        picture.setStatus(2);  //状态为2的是封面
        //查询出之前的封面
        Example example = new Example(Picture.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 2);
        Picture _picture = pictureMapper.selectOneByExample(example);
        if (_picture != null) {
            if (picture.getUid().equals(_picture.getUid())) {
                throw new RuntimeException("该图片已经是封面!");
            }
            _picture.setStatus(1);  //将当前状态设置为1  非封面
            pictureMapper.updateByPrimaryKeySelective(_picture);
        }
        //将当前图片设置为封面
        pictureMapper.updateByPrimaryKeySelective(picture);
    }


    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Picture.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andLike("uid", "%" + searchMap.get("uid") + "%");
            }
            if (searchMap.get("fileUid") != null && !"".equals(searchMap.get("fileUid"))) {
                criteria.andLike("fileUid", "%" + searchMap.get("fileUid") + "%");
            }
            if (searchMap.get("picName") != null && !"".equals(searchMap.get("picName"))) {
                criteria.andLike("picName", "%" + searchMap.get("picName") + "%");
            }
            if (searchMap.get("pictureSortUid") != null && !"".equals(searchMap.get("pictureSortUid"))) {
                criteria.andLike("pictureSortUid", "%" + searchMap.get("pictureSortUid") + "%");
            }
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            if (searchMap.get("startTime") != null) {
                criteria.andGreaterThan("createTime", searchMap.get("startTime"));
            }
        }
        return example;
    }
}
